 //控制层 
app.controller('itemCatController' ,function($scope,$controller   ,itemCatService){	
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		itemCatService.findAll().success(
			function(response){
				$scope.list=response.list;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		itemCatService.findPage(page,rows).success(
			function(response){
				$scope.list=response.list;
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		itemCatService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	}
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=itemCatService.update( $scope.entity ); //修改  
		}else{
			serviceObject=itemCatService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	$scope.reloadList();//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}
	
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框
		if (confirm("是否确认删除？")) {
            itemCatService.dele( $scope.selectIds ).success(
                function(response){
                    if(response.success){
                        // $scope.reloadList();
                        $scope.findByParentId($scope.parentId) //重新加载
                        $scope.selectIds=[];
                    }
                }
            );
		}
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		itemCatService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.list;
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}

	//根据上级id查询商品分类列表
	$scope.findByParentId=function (parentId) {

		$scope.parentId=parentId; //查询时记住上级id
		itemCatService.findByParentId(parentId).success(
			function (data) {
				$scope.list=data;
            }
		)
    }

//面包屑开始:

    //设置级别
    $scope.grade=1;//默认为1级

    $scope.setGrade=function (value) {
        $scope.grade=value;
    }

    //读取分类列表
    $scope.selectList=function (p_entity) {
        if ($scope.grade == 1) {  //如果为1级

            $scope.entity_1=null;
            $scope.entity_2=null;
        }

        if ($scope.grade == 2) { //如果为2级
            $scope.entity_1=p_entity;
            $scope.entity_2=null;
        }

        if ($scope.grade == 3) { //如果为3级
            $scope.entity_2=p_entity;
        }

        $scope.findByParentId(p_entity.id); //查询此级下级列表
    }
//面包屑结束


	//保存
	$scope.save=function () {
		var serviceObject; //服务层对象
		if ($scope.entity.id != null) { //如果有id
            serviceObject=itemCatService.update($scope.entity); //修改
		}else { //如果没有id
			$scope.entity.parentId=$scope.parentId;
			serviceObject=itemCatService.add($scope.entity); //添加
		}

		serviceObject.success(
			function (data) {
				if (data.success) {
					$scope.findByParentId($scope.parentId) //重新加载
				}else {
					alert(data.message)
				}
            }
		)
    }
});	
